package com.xsh.gls.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsh.gls.pojo.Depot;
import com.xsh.gls.pojo.Transfer;
import com.xsh.gls.pojo.User;
import com.xsh.gls.pojo.page;
import com.xsh.gls.service.DepotService;
import com.xsh.gls.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/depot")
public class DepotController {
    @Autowired
    private DepotService depotService;
    @Autowired
    private UserService userService;

    @RequestMapping("/selectdepot")
    public Object select(@RequestParam(required = false)String depotname,  @RequestParam(required = false)String pageNum,
                          HttpServletRequest request, HttpServletResponse response){


        Map<String,Object> params=new HashMap<String,Object>();
        System.out.println(depotname);
        List<Depot> depotList=null;
        Integer _pageNum=1;
        if(pageNum!=null&&!pageNum.equals("")){
            _pageNum=Integer.parseInt(pageNum);
        }
        PageHelper.startPage(_pageNum,5, "D_CREATEDATE desc");

        try {
            depotList=depotService.selectByDepotName(depotname);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo<Depot> page=new PageInfo<Depot>(depotList);

       /* List<Depot> list=page.getList();*/
        return page;
    }

    @RequestMapping("/insert")
    public Object select(){
        List<User> users = userService.selectByTypeID(3);
        return users;
    }

    @RequestMapping("/add")
    public Transfer add(Depot depot){
        Transfer transfer=new Transfer();
        depot.setdCreatedate(new Date());
        boolean a=depotService.add(depot);
        if (a){
            System.out.println("添加成功");
            transfer.setMessage("yes");

        }else {
            System.out.println("添加失败");
            transfer.setMessage("no");
        }
        return transfer;
    }

    @RequestMapping("/update")
    public Transfer update(Depot depot){
        Transfer transfer=new Transfer();
        System.out.println(depot.getdAddress());
       ;/* SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss*")/
        /*try {
            Date dCreatedate = format.parse(dCreatedate1);
            depot.setdCreatedate(dCreatedate);
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        depot.setdCreatedate(new Date());
        boolean b=depotService.update(depot);
        if (b){
            System.out.println("修改成功");
            transfer.setMessage("yes");
        }else {
            System.out.println("修改失败");
            transfer.setMessage("no");
        }
        return transfer;
    }

    @RequestMapping("/selectById")
    public Object selectById(Integer id){
        Depot depot=new Depot();
        depot=depotService.selectById(id);
        depot.setUnames(userService.selectByTypeID(3));
        return depot;

    }

    @RequestMapping("/delete")
    public Transfer delete(Integer id){
        Transfer transfer=new Transfer();
        boolean d=depotService.delete(id);
        if (d){
            System.out.println("删除成功");
            transfer.setMessage("yes");
        }else{
            System.out.println("删除失败");
            transfer.setMessage("no");
        }
        return transfer;
    }
}
